$(function () {

    var obj = uploadImgFile({
        selectObj:'#feedback-upload-box',
        imgNum:4,
        param:'feedback',
        previewCallback:function (file,src) {
            $("#tqImgWrap").append('<span><em class="wm-close-em" data-id="'+file.id+'"><i class="iconfont icon-guanbi"></i></em><img src="'+src+'" width="75px" height="50px"/></span>');
            $("#def-span-box").hide();
        },
        uploadFinished:function (urlList) {
            var imageUrls="";
            $.each(urlList,function (index, e) {
                if(index < urlList.length -1){
                    imageUrls+= e+",";
                }else{
                    imageUrls+= e;
                }
            });
            $("#imageStr").val(imageUrls);
            s = true;
            //上传完成后提交数据
            saveFeedback();
        }
    });

    //提问方法
    $("body").on("click","#saveFeedBack",function () {
        //判断是否有图片上传
        var files = obj.getFiles('inited');
        if(files.length > 0){//所有文件先上传文件
            $("#tqImgWrap>span>em").remove();
            obj.upload();
        }else{//没有文件可直接保存问答内容
            saveFeedback();
        }
    }).on('click','#tqImgWrap>span>em',function () {
        var _this = $(this);
        var fileId = _this.data("id");
        obj.removeFile(fileId);
        _this.parent().remove();
        //获取初化状态的文件
        var files = obj.getFiles('inited');
        //如果队列中没有文件
        if(files.length<=0){
            $("#def-span-box").show();
        }
    });
    //防止重新提交状态
    var s = true;
    function saveFeedback() {
        var params = $("#saveFeedBackForm").serializeJSON();
        params = JSON.stringify(params);
        //去获取图片url
        $.ajax({
            url: baseloaction + "/mobile/uc/op/feedback/saveUpdateFeedBack",
            data:params,
            type:"post",
            dataType:"json",
            contentType: 'application/json;charset=utf-8',
            success:function (result) {
                if(result.success){
                    $.alert(result.message,function () {
                        window.location.reload();
                    });
                }else {
                    s = true;
                    $.toast(result.message);
                }
            },
            error:function (error) {
                s = true;
                $.toast("系统繁忙，请稍后重试");
            }
        });
    }
});

